package com.swing.gui;

import com.swing.assets.Iurl;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * @author Yuang, Gongming
 * @date 2022-01-19 13:15
 */
public class Settings extends JDialog {
    private static Settings settings;

    private Settings(){}

    public static Settings init(){
        if(settings == null){
            synchronized (Settings.class){
                if(settings == null){
                    settings = new Settings();
                    try {
                        template();
                    } catch (MalformedURLException e) {
                        //  ignore
                    }
                    listener();
                }
            }
        }
        return settings;
    }

    private static void template() throws MalformedURLException {
        ImageIcon icon = new ImageIcon(new URL(Iurl.SETTINGS));
        icon.setImage(icon.getImage().getScaledInstance(20, 20, Image.SCALE_AREA_AVERAGING));
        settings.setIconImage(icon.getImage());
        settings.setTitle("设置");
        settings.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
        settings.setSize(APP.init().getWidth() / 2, APP.init().getWidth() / 2);
        settings.setLocationRelativeTo(APP.init());
        settings.setAlwaysOnTop(true);
    }

    private static void listener() {
        settings.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                settings.setVisible(false);
            }

        });

        settings.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getButton() == MouseEvent.BUTTON3){
                    if (3 < e.getClickCount()) {
                        JOptionPane.showMessageDialog(settings, "敬请期待……");
                    }
                }
            }
        });
    }

}
